package bb.lanxing.util.io;

import android.content.Context;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

import bb.lanxing.model.database.ITrackPoint;
import bb.lanxing.model.database.Workout;
import gov.nist.core.Separators;

public class GpxTrackWriter {
    private static final NumberFormat COORDINATE_FORMAT;
    private static final NumberFormat ELEVATION_FORMAT;
    private static final SimpleDateFormat ISO_8601_DATE_TIME_FORMAT;
    private final Context context;
    private PrintWriter printWriter;

    static {
        NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);
        ELEVATION_FORMAT = numberFormat;
        NumberFormat numberFormat2 = NumberFormat.getInstance(Locale.US);
        COORDINATE_FORMAT = numberFormat2;
        ISO_8601_DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
        numberFormat.setMaximumFractionDigits(1);
        numberFormat.setGroupingUsed(false);
        numberFormat2.setMaximumFractionDigits(6);
        numberFormat2.setMaximumIntegerDigits(3);
        numberFormat2.setGroupingUsed(false);
    }

    public GpxTrackWriter(Context context) {
        this.context = context;
    }

    public void prepare(OutputStream outputStream) {
        this.printWriter = new PrintWriter(outputStream);
    }

    public void close() {
        PrintWriter printWriter = this.printWriter;
        if (printWriter != null) {
            printWriter.close();
            this.printWriter = null;
        }
    }

    public void writeHeader(Workout workout) {
        PrintWriter printWriter = this.printWriter;
        if (printWriter != null) {
            printWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            this.printWriter.println("<gpx");
            this.printWriter.println("version=\"1.1\"");
            this.printWriter.println("creator=\"蓝行\"");
            this.printWriter.println("xmlns=\"http://www.topografix.com/GPX/1/1\"");
            this.printWriter.println("xmlns:topografix=\"http://www.topografix.com/GPX/Private/TopoGrafix/0/1\"");
            this.printWriter.println("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
            this.printWriter.println("xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.topografix.com/GPX/Private/TopoGrafix/0/1 http://www.topografix.com/GPX/Private/TopoGrafix/0/1/topografix.xsd\">");
            this.printWriter.println("<metadata>");
            PrintWriter printWriter2 = this.printWriter;
            printWriter2.println("<name>" + formatCData("蓝行记录") + "</name>");
            PrintWriter printWriter3 = this.printWriter;
            printWriter3.println("<desc>" + formatCData(workout.getTitle()) + "</desc>");
            this.printWriter.println("</metadata>");
        }
    }

    public void writeFooter() {
        PrintWriter printWriter = this.printWriter;
        if (printWriter != null) {
            printWriter.println("</gpx>");
        }
    }

    public void writeBeginTrack(Workout workout) {
        PrintWriter printWriter = this.printWriter;
        if (printWriter != null) {
            printWriter.println("<trk>");
            PrintWriter printWriter2 = this.printWriter;
            printWriter2.println("<name>" + formatCData("蓝行记录") + "</name>");
            PrintWriter printWriter3 = this.printWriter;
            printWriter3.println("<desc>" + formatCData(workout.getTitle()) + "</desc>");
        }
    }

    public void writeEndTrack() {
        PrintWriter printWriter = this.printWriter;
        if (printWriter != null) {
            printWriter.println("</trk>");
        }
    }

    public void writeOpenSegment() {
        this.printWriter.println("<trkseg>");
    }

    public void writeCloseSegment() {
        this.printWriter.println("</trkseg>");
    }

    public void writePoint(ITrackPoint iTrackPoint) {
        PrintWriter printWriter = this.printWriter;
        if (printWriter != null) {
            printWriter.println("<trkpt " + formatLocation(iTrackPoint) + Separators.GREATER_THAN);
            PrintWriter printWriter2 = this.printWriter;
            printWriter2.println("<ele>" + ELEVATION_FORMAT.format(iTrackPoint.getAltitude()) + "</ele>");
            PrintWriter printWriter3 = this.printWriter;
            printWriter3.println("<time>" + formatDateTimeIso8601(iTrackPoint.getTime()) + "</time>");
            PrintWriter printWriter4 = this.printWriter;
            printWriter4.println("<speed>" + iTrackPoint.getSpeed() + "</speed>");
            this.printWriter.println("</trkpt>");
        }
    }

    private String formatLocation(ITrackPoint iTrackPoint) {
        NumberFormat numberFormat = COORDINATE_FORMAT;
        String sb = "lat=\"" +
                numberFormat.format(iTrackPoint.getLatitude()) +
                "\" lon=\"" +
                numberFormat.format(iTrackPoint.getLongitude()) +
                "\"";
        return sb;
    }

    public static String formatCData(String str) {
        if (str == null) {
            str = "";
        }
        return "<![CDATA[" + str.replaceAll("]]>", "]]]]><![CDATA[>") + "]]>";
    }

    public static String formatDateTimeIso8601(long j) {
        return ISO_8601_DATE_TIME_FORMAT.format(j);
    }
}